package com.hn658.user.backend.action.department.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.hn658.framework.common.AbstractBaseService;
import com.hn658.framework.common.IServiceResponse;
import com.hn658.framework.dataaccess.pagination.GenericPage;
import com.hn658.framework.shared.exception.BusinessException;
import com.hn658.user.backend.action.department.IDepartmentAction;
import com.hn658.user.backend.action.department.vo.DepartmentRequest;
import com.hn658.user.backend.action.department.vo.DepartmentResponse;
import com.hn658.user.backend.action.department.vo.EmployeeRequest;
import com.hn658.user.backend.action.department.vo.EmployeeResponse;
import com.hn658.user.common.entity.DepartmentEO;
import com.hn658.user.common.entity.EmployeeEO;
import com.hn658.user.common.entity.TreeNode;
import com.hn658.user.common.vo.CommonResponse;
import com.hn658.user.core.service.IDepartmentService;
import com.hn658.user.core.service.IEmployeeService;
import com.hn658.user.core.service.ISync;

@Controller("departmentAction")
@Path("department")
@Produces("application/json; charset=UTF-8")
@Consumes("application/json; charset=UTF-8")
public class DepartmentAction extends AbstractBaseService implements
		IDepartmentAction {
	@Autowired
	private IDepartmentService departmentService;
	
	@Autowired
	private IEmployeeService employeeService;
	
	@Autowired
	private ISync sync;
	
	@Path("sync")
	@POST
	public IServiceResponse sync(DepartmentRequest request) {
		CommonResponse response = new CommonResponse();
		try {
			sync.departmentSync();
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}
	
	@Path("syncEmployee")
	@POST
	public IServiceResponse syncEmployee(EmployeeRequest request) {
		CommonResponse response = new CommonResponse();
		try {
			sync.employeeSync();
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}
	
	@SuppressWarnings("rawtypes")
	@Path("queryDepartmentList")
	@Consumes("application/x-www-form-urlencoded; charset=UTF-8")
	@Produces("application/json; charset=UTF-8")
	@POST
	public IServiceResponse queryFeedbackList(
			@FormParam(value = "id") String parentDeptId,
			@FormParam(value = "limit") Integer limit,
			@FormParam(value = "start") Integer start) {
		DepartmentResponse response = new DepartmentResponse();
		try {
			if (StringUtils.isBlank(parentDeptId))
				parentDeptId = "1";
			List<DepartmentEO> deptEntities = departmentService
					.getDirectChildrenDepts(parentDeptId, false);
			LinkedHashSet<TreeNode> nodes = getTreeNodes(deptEntities);
			response.setNodes(nodes);
			if (limit == null)
				limit = 20;
			if (start == null)
				start = 0;
			Map<String, Object> map=new HashMap<String, Object>();
			map.put("parentDeptId", parentDeptId);
			GenericPage<EmployeeEO> page = employeeService
					.queryEmployee(map, limit,
							start, "uid", false);
			response.setEmployeeList(page.getData());
			response.setTotalCount(page.getTotalCount());
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}
	
	/**
	 * 分页查询员工信息
	 * @param code
	 * @param limit
	 * @param start
	 * @return
	 */
	@Path("queryEmployeeList")
	@Consumes("application/x-www-form-urlencoded; charset=UTF-8")
	@Produces("application/json; charset=UTF-8")
	@POST
	public IServiceResponse queryEmployeeList(
			@FormParam(value = "id") String deptId,
			@FormParam(value = "email") String email,
			@FormParam(value = "empName") String empName,
			@FormParam(value = "limit") int limit,
			@FormParam(value = "start") int start) {
		DepartmentResponse response = new DepartmentResponse();
		HashMap<String, Object> queryParam = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(deptId)) {
			queryParam.put("deptId", Long.parseLong(deptId.trim()));
		}
		if (StringUtils.isNotBlank(email)) {
			queryParam.put("email", email);
		}
		if (StringUtils.isNotBlank(empName)) {
			queryParam.put("empName", empName);
		}
		try {
			 GenericPage<EmployeeEO> queryEmployee = employeeService.queryEmployee(
					queryParam, limit, start, "uid", false);
			response.setEmployeeList(queryEmployee.getData());
			response.setTotalCount(queryEmployee.getTotalCount());
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}

	@Path("addDepartment")
	@POST
	public IServiceResponse addDepartment(DepartmentRequest request) {
		DepartmentResponse response = new DepartmentResponse();
		try {
			DepartmentEO departmentEO=request.getDepartmentEO();
			departmentEO.setCreateTime(new Date());
			departmentService.addDepartment(departmentEO);
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}

	@Path("modifyDepartment")
	@POST
	public IServiceResponse modifyDepartment(DepartmentRequest request) {
		DepartmentResponse response = new DepartmentResponse();
		try {
			DepartmentEO departmentEO=request.getDepartmentEO();
			departmentEO.setLastUpdateTime(new Date());
			departmentService.modifyDepartment(departmentEO);
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}

	@Path("deleteDepartment")
	@POST
	public IServiceResponse deleteDepartment(DepartmentRequest request) {
		DepartmentResponse response = new DepartmentResponse();
		try {
			departmentService.deleteDepartment(request.getDepartmentEO().getId());
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e.getErrorMsg());
		}
		return response;
	}

	@SuppressWarnings("rawtypes")
	private LinkedHashSet<TreeNode> getTreeNodes(List<DepartmentEO> list) {
		LinkedHashSet<TreeNode> nodes = new LinkedHashSet<TreeNode>();
		for (DepartmentEO entity : list) {
			TreeNode<DepartmentEO, TreeNode> treeNode = new TreeNode<DepartmentEO, TreeNode>();
			treeNode.setId(entity.getId());
			treeNode.setText(entity.getDeptName());
			List<DepartmentEO> children = departmentService
					.getDirectChildrenDepts(entity.getId().toString(), false);
			if (children.size() > 0)
				treeNode.setLeaf(false);
			else
				treeNode.setLeaf(true);
			treeNode.setParentId(entity.getParentDeptId());
			treeNode.setEntity(entity);
			treeNode.setChildren(getTreeNodes(children));
			nodes.add(treeNode);
		}
		return nodes;
	}
	@Path("addEmployee")
	@POST
	public IServiceResponse addEmployee(
			EmployeeRequest request) {
		EmployeeResponse response = new EmployeeResponse();
		try {
			EmployeeEO employeeEO = request.getEmployeeEO();
			employeeEO.setCreateTime(new Date());
			employeeEO.setDeptId(request.getEmployeeEO().getDeptId());
			employeeService.addEmployee(employeeEO);
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}
	@Path("deleteEmployee")
	@POST
	public IServiceResponse deleteEmployee(EmployeeRequest request){
		EmployeeResponse response=new EmployeeResponse();
		try {
			
			employeeService.deleteEmployee(request.getEmployeeEO().getUid());
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
		
	}
	@Path("updateEmployee")
	@POST 
	public IServiceResponse updateEmployee(EmployeeRequest request){
		EmployeeResponse response=new EmployeeResponse();
		try {
			EmployeeEO employeeEO = request.getEmployeeEO();
			employeeEO.setLastUpdateTime(new Date());
			employeeEO.setDeptId(request.getEmployeeEO().getDeptId());
			employeeService.updateEmployee(employeeEO);
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
		
	}
	
}
